package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.MessageEntity;
import com.onetime.mall.trading.service.MessageService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import com.onetime.mall.trading.vo.MessageAndUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 留言表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = messageService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/idle")
    public R getAllIdleMessage(@RequestParam Long idleId) {
        List<MessageAndUserVo> list = messageService.getAllIdleMessage(idleId);
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MessageEntity message = messageService.getById(id);

        return R.ok().put("message", message);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MessageEntity message) {
        messageService.save(message);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MessageEntity message) {
        messageService.updateById(message);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete1")
    public R delete(@RequestBody Long[] ids) {
        messageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除消息
     *
     * @param shUserId 用户id
     * @param id       消息ID
     * @return JSON
     */
    @GetMapping("/delete")
    public R deleteMessage(@RequestParam("userId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                           @RequestParam Long id) {
        if (messageService.deleteMessage(id)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    @GetMapping("/my")
    public R getAllMyMessage(@RequestParam("userId")
                             @NotNull(message = "登录异常 请重新登录")
                             @NotEmpty(message = "登录异常 请重新登录") String shUserId) {
        Long userId = Long.valueOf(shUserId);
        System.out.println("我的消息："+userId);
        List<MessageAndUserVo> list = messageService.getAllMyMessage(userId);
        return R.ok().put("data", list);
    }

    @GetMapping("/info")
    public R getMessage(@RequestParam Long id) {
        MessageEntity msg = messageService.getMessage(id);
        return R.ok().put("data", msg);
    }


    @PostMapping("/send")
    public R sendMessage(@RequestParam("userId")
                         @NotNull(message = "登录异常 请重新登录")
                         @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                         @RequestBody MessageEntity messageEntity) {
        messageEntity.setUserId(Long.valueOf(shUserId));
        messageEntity.setCreateTime(new Date());
        if (messageService.addMessage(messageEntity)) {
            return R.ok().put("data", messageEntity);
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }


}
